<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    $mes ="";
    if (isset($_SESSION['mes'])) {
        $mes=$_SESSION['mes'];
        $_SESSION['mes']="";
    }
    include "../include/dbFunctions.php";
    include "../include/formFunctions.php";
    include "../include/menu.php";
    $_SESSION['eid']=$id = $_GET['id'];
//get general data
    $query1= "SELECT e.contact_id, e.event_class_level, e.event_name, e.event_status, p.program_id, p.program_name, s.school_id, s.school_name, s.school_address, s.school_postal, p.program_lesson_duration
                FROM event e, school s, program p, contact_school c
                WHERE e.event_id =$id
                AND c.contact_school_id = e.contact_id
                AND c.school_id = s.school_id
                AND e.program_id = p.program_id";
    $event_array = executeSelectQuery($query1);
    $name = $event_array[0]['event_name'];
    $status = $event_array[0]['event_status'];
    $program = $event_array[0]['program_name'];
    $school = $event_array[0]['school_name'];
    $location = $event_array[0]['school_address'];
    $postal = $event_array[0]['school_postal'];
    $duration = $event_array[0]['program_lesson_duration'];
    $level = $event_array[0]['event_class_level'];
    $csid = $event_array[0]['contact_id'];
// get session data
    $query2 = "SELECT * FROM session WHERE event_id=$id";
    $session_array = executeSelectQuery($query2);
// get class data
    $query2 = "SELECT c.class_name, c.class_no_student, c.class_student_list, c.class_id
                FROM class c
                WHERE c.event_id =$id";
    $class_array = executeSelectQuery($query2);
// get volunteer data
    $query3 = "SELECT *
                FROM volunteer v, trainer t
                WHERE v.volunteer_id=t.volunteer_id
                AND t.event_id = $id";
    $volunteer_array = executeSelectQuery($query3);
//get tracking data
    $query4 = "SELECT DISTINCT tc.track_criteria,tc.track_id, td.track_detail, td.last_modified
                FROM track_detail td, track_criteria tc
                WHERE event_id = $id AND td.track_id = tc.track_id";
    $track_array = executeSelectQuery($query4);
    $query5 = "SELECT contact_name, contact_email FROM contact_school WHERE contact_school_id=$csid";
    $contact_array = executeSelectQuery($query5);
    $contact_name=$contact_array[0]['contact_name'];
    $contact_email=$contact_array[0]['contact_email'];
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JA Scheduling</title>

        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/dataTable.js"></script>

        <!--  CSS script -->
        <link rel="stylesheet" href="../media/css/screen.css" type="text/css" media="screen" title="default" />
        <link type="text/css" href="../media/css/dataTable.css" rel="stylesheet" />
        <link rel="stylesheet" type="text/css" href="../media/css/jquery-ui-1.8.13.custom.css" />
        <script type="text/javascript" src="../media/js/jquery/jquery-ui-1.8.13.custom.js"></script>
        <script type="text/javascript" src="../media/js/jquery/jquery-ui-timepicker-addon.js"></script>
        <!-- Manage table script -->
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
                $('#admintable').dataTable( {
                    "sPaginationType": "full_numbers"
                } );
            });
        </script>
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
                $('.date').datepicker();
                $('.time').timepicker({});
            });
        </script>
    </head>
    <body>
        <!-- DON'T TOUCH! Start: page-top-outer -->
            <?php
            top_outer();
            ?>
        <!-- End: page-top-outer -->

        <div class="clear">&nbsp;</div>

        <!--  start nav-outer-repeat................................................................................................. START -->
        <div class="nav-outer-repeat">
            <!--  start nav-outer -->
            <div class="nav-outer">

                <!-- start nav-right -->
                    <?php
                    nav_right();
                    ?>
                <!-- end nav-right -->


                <!--  start nav -->
                <div class="nav">
                    <div class="table">

                            <?php
                            navigation_event();
                            ?>

                        <div class="clear"></div>
                    </div>
                    <div class="clear"></div>
                </div>
                <!--  start nav -->

            </div>
            <div class="clear"></div>
            <!--  start nav-outer -->
        </div>
        <!--  start nav-outer-repeat................................................... END -->

        <div class="clear"></div>

        <!-- start content-outer ........................................................................................................................START -->
        <div id="content-outer">
            <!-- start content -->
            <div id="content">

                <!--  start page-heading --> <!--lyedit-->
                <div id="page-heading">
                    <h1>View Course <?php if($status =='requested') { ?>
                        <a href="action/eventConfirm.php"><img src='../media/images/forms/form_confirm.gif' height ='25'></img></a>
                                <?php }elseif ($status=='pending') {
                                echo <<<HERE
                                    <a href="action/eventFinalize.php"><img src='../media/images/forms/finalize_event.gif' height ='25'></img></a>
HERE;
                            } ?> </h1>
                </div>
                <!-- end page-heading -->

                <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                    <tr>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                        <th class="topleft"></th>
                        <td id="tbl-border-top">&nbsp;</td>
                        <th class="topright"></th>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                    </tr>
                    <tr>
                        <td id="tbl-border-left"></td>
                        <td>
                            <!--  start content-table-inner ...................................................................... START -->
                            <div id="content-table-inner">
                                <h2></h2>
                                <!--  start table-content  -->
                                <div id="table-content">
                                    <!--  Can put table start from here within this div-->
                                    <font color="red"><b><?php echo $mes?></b></font>
                                    <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
                                        <tr>
                                            <td colspan="2"><br/><h3>General Information  <a href="eventUpdateGeneral.php"><img src="../media/images/forms/edit-icon.png" width=15></img></a></h3></td>
                                        </tr>
                                        <tr>
                                            <th>Course Name:</th>
                                            <td><?php echo $name;?></td>
                                        </tr>
                                        <tr>
                                            <th>Course  Level:</th>
                                            <td><?php echo $level;?></td>
                                        </tr>
                                        <tr>
                                            <th>Course Status:</th>
                                            <td><?php echo eventStatus($status);?></td>
                                        </tr>
                                        <tr>
                                            <th>Program Name:</th>
                                            <td><a href="../program/programView.php?id=<?php echo $event_array[0]['program_id']?>"><?php echo ucfirst($program);?></a></td>
                                        </tr>
                                        <tr>
                                            <th>School Name:</th>
                                            <td><a href="../school/schoolView.php?id=<?php echo $event_array[0]['school_id']?>"><?php echo ucfirst($school);?></a></td>
                                        </tr>
                                        <tr>
                                            <th>Contact Email:</th>
                                            <td><a href='mailto:<?php echo $contact_email?>?Subject=From JA Admin:'><?php echo $contact_email?></a></td>
                                        </tr>
                                        <tr>
                                            <th>School Address:</th>
                                            <td><?php echo $location;?></td>
                                        </tr>
                                        <tr>
                                            <th>School Postal Code:</th>
                                            <td><?php echo $postal;?></td>
                                        </tr>
                                        <!--Tracking-->
                                        <tr>
                                            <td colspan="2"><br/><h3>Tracking </h3></td>
                                        </tr>
                                        <tr>
                                            <th></th>
                                            <td>

                                            </td>
                                        </tr>
                                        <!--Session-->
                                    </table>
                                    <table border="1">
                                        <tr>
                                            <th class="thmain">No.</th>
                                            <th class="thmain">Criteria</th>
                                            <th class="thmain">Track</th>
                                            <th class="thmain">Action</th>
                                            <th class="thmain">Last modified</th>
                                        </tr>
                                            <?php
                                            for ($i=0;$i<count($track_array);$i++) {
                                                $tid = $track_array[$i]['track_id'];
                                                $no = $tid+1;
                                                echo "<tr>";
                                                echo "<td class='tdmain'>".$no."</td>";
                                                echo "<td class='tdmain'>".$track_array[$i]['track_criteria']."</td>";
                                                if ($track_array[$i]['track_detail']==0) {
                                                    echo "<td class='tdmain'><img src='../media/images/forms/cross_icon.png' height ='20'></td>";

                                                    echo <<<HERE
                                                                    <td class='tdmain'><form action='action/eventDoTrack.php' method='post'>
                                                                        <input type ='hidden' name='tid' value=$tid>
                                                                            <table>
                                                                                <tr>
                                                                                    <td><input type='text' size="11" class='date' name='date'></td>
                                                                                    <td><input type = 'submit' value = 'Go'></td>
                                                                                </tr>
                                                                            </table>
                                                                    </form></td>
HERE;

                                                    echo "<td class='tdmain'>".$track_array[$i]['last_modified']."</td>";
                                                } else {
                                                    echo "<td class='tdmain'><img src='../media/images/forms/check_icon.png' height ='20'></td>";
                                                    echo <<<HERE
                                                                    <td class='tdmain'><form action='action/eventUndoTrack.php' method='post'>
                                                                        <input type ='hidden' name='tid' value=$tid>
                                                                        <input type = 'submit' value = 'Undo'>
                                                                    </form></td>
HERE;
                                                    echo "<td class='tdmain'>".$track_array[$i]['last_modified']."</td>";
                                                }
                                                echo "</tr>";
                                            }
                                            ?>
                                    </table>
                                    <table>
                                        <tr>
                                            <td colspan="2"><br/><h3>Sessions<a href="eventUpdateSession.php"><img src="../media/images/forms/edit-icon.png" width=15></img></a></h3></td>
                                        </tr>
                                        <tr>
                                            <th></th>
                                            <td>
                                                <table border="1">
                                                    <tr>
                                                        <th class='thmain'>No.</th>
                                                        <th class='thmain'>Date</th>
                                                        <th class='thmain'>Start Time</th>
                                                        <th class='thmain'>End Time</th>
                                                    </tr>
                                                        <?php
                                                        for ($i=0;$i<count($session_array);$i++) {
                                                            $no=$i+1;
                                                            $date = $session_array[$i]['session_date'];
                                                            $start_time = $session_array[$i]['session_time'];
                                                            $end_time=$session_array[$i]['session_end_time'];
                                                            echo "<tr>";
                                                            echo "<td class='tdmain'>$no</td>";
                                                            echo "<td class='tdmain'>$date</td>";
                                                            echo "<td class='tdmain'>$start_time</td>";
                                                            echo "<td class='tdmain'>$end_time</td>";
                                                            echo "</tr>";
                                                        }
                                                        ?>
                                                </table>
                                            </td>
                                        </tr>
                                        <!--Student info-->
                                        <tr>
                                            <td colspan="2">
                                                <table>
                                                    <tr>
                                                        <h3><br/>Classes
                                                            <a href="eventAddClass.php"><img src="../media/images/forms/add-icon.png" width=17></img></a></h3>
                                                    </tr>
                                                    <tr>
                                                        <th></th>
                                                        <td>
                                                            <table border="1">
                                                                <tr>
                                                                    <th class='thmain'>No.</th>
                                                                    <th class='thmain'>Class Name</th>
                                                                    <th class='thmain'>No. Student</th>
                                                                    <th class='thmain'>Student File</th>
                                                                    <th class='thmain'>Volunteer</th>
                                                                    <th class='thmain'>Action</th>
                                                                </tr>
                                                                    <?php
                                                                    for ($i=0;$i<count($class_array);$i++) {
                                                                        $no=$i+1;
                                                                        $cid = $class_array[$i]['class_id'];
                                                                        $name = $class_array[$i]['class_name'];
                                                                        $no_student = $class_array[$i]['class_no_student'];
                                                                        $file = $class_array[$i]['class_student_list'];
                                                                        $classid = $class_array[$i]['class_id'];
                                                                        echo "<tr>";
                                                                        echo "<td class='tdmain'>$no</td>";
                                                                        echo "<td class='tdmain'>$name</td>";
                                                                        echo "<td class='tdmain'>$no_student</td>";
                                                                        if ($file!="") {
                                                                            echo "<td class='tdmain'><a href='../upload/$file'>Download</a></td>";
                                                                        } else {
                                                                            echo "<td class='tdmain'>No file</td>";
                                                                        }
                                                                        // get volunteer data
                                                                        $query2 = "SELECT v.volunteer_surname, v.volunteer_firstname, v.volunteer_id
                                                                                    FROM trainer t, volunteer v
                                                                                    WHERE t.volunteer_id = v.volunteer_id
                                                                                    AND t.trainer_status= 'confirmed'
                                                                                    AND t.event_id=$id
                                                                                    AND t.class_id =$cid";
                                                                        $trainer_array = executeSelectQuery($query2);
                                                                        for ($j=0;$j<count($trainer_array);$j++) {
                                                                            $tid = $trainer_array[$j]['volunteer_id'];
                                                                            $trainer_array[$j]['volunteer_name']=$trainer_array[$j]['volunteer_firstname']." ".$trainer_array[$j]['volunteer_surname'];
                                                                            $link = <<<HERE
                                                        <a onclick="return  confirm('Trainer will be removed from this class. No email will be sent. Do you wish to continue?')" href='action/eventDeleteTrainer.php?id=$tid'><img src='../media/images/forms/delete-icon.png' height ='12'></a>
HERE;
                                                                            $trainer_array[$j]['volunteer_name']= $trainer_array[$j]['volunteer_name'].$link;
                                                                        }
                                                                        echo "<td class='tdmain'>";
                                                                        for ($t=0;$t<count($trainer_array);$t++) {
                                                                            $trainer_name = "<a href = ../volunteer/volunteerView.php?id=".$trainer_array[$t]['volunteer_id'].">".$trainer_array[$t]['volunteer_name']."</a>";
                                                                            echo $trainer_name."<br/>";
                                                                        }
                                                                        echo "</td>";
                                                                        echo <<<HERE
                                                                        <td class='tdmain'><a href='eventUpdateClass.php?id=$cid'><img src='../media/images/forms/edit-icon.png' width=15></img></a>
                                                                        <a onclick="return  confirm('This class will be removed permanently. No email will be sent. Do you wish to continue?')" href='action/eventDeleteClass.php?id=$cid'><img src='../media/images/forms/delete-icon.png' height ='14'></a>
                                                                        </td>
HERE;
                                                                        echo "</tr>";
                                                                    }
                                                                    ?>
                                                            </table>
                                                        </td>

                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>

                                    </table>
                                    <br/>
                                    <!--Volunteers-->

                                        <?php if (($status!='cancelled')&&($status!='confirmed')) {?>
                                    <h3>Volunteers</h3>
                                    <h4>Volunteers Accepting Requests</h4>
                                    <table cellpadding="0" cellspacing="0" border="0" class="display" id="admintable">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Volunteer</th>
                                                <th>Email</th>
                                                <th>Mobile number</th>
                                                <th>Assign Class</th>
                                                <th>Reject</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                                    <?php
                                                    for ($i=0;$i<count($volunteer_array);$i++) {
                                                        $no =$i+1;
                                                        if ($volunteer_array[$i]['trainer_status']=='accept') {
                                                            $volunteer_id = $volunteer_array[$i]['volunteer_id'];
                                                            $volunteer = $volunteer_array[$i]['volunteer_firstname']." ".$volunteer_array[$i]['volunteer_surname'];
                                                            $email = $volunteer_array[$i]['volunteer_email'];
                                                            $mobile = $volunteer_array[$i]['volunteer_mobile'];
                                                            echo "<tr>";
                                                            echo "<td>$no</td>";
                                                            echo "<td><a href='../volunteer/volunteerView.php?id=".$volunteer_id."'>".$volunteer."</td>";
                                                            echo "<td><a href='mailto:$email?Subject=From JA Admin:'>".$email."</a></td>";
                                                            echo "<td>$mobile</td>";
                                                            echo "<td><form action = 'action/eventDoAssignClass.php' method ='post'>";
                                                            echo "<input type ='hidden' value=$volunteer_id name = 'vid'>";
                                                            echo "<select name='cid'>";
                                                            echo "<option>----</option>";
                                                            for ($j=0;$j<count($class_array);$j++) {
                                                                $class_id = $class_array[$j]['class_id'];
                                                                $class_name = $class_array[$j]['class_name'];
                                                                echo "<option value='$class_id'>$class_name</option>";
                                                            }
                                                            echo "</select>";
                                                            echo "<input type='submit' value='Assign'></td></form></td>";
                                                            echo <<<HERE
                                                            <td><form action='action/eventRejectTrainer.php' method='post'>
                                                                <input type ='hidden' name='vid' value = $volunteer_id ></input>
                                                                <input type='submit' value='Reject' class= 'form-reject' onclick="return confirm('This volunteer will be removed from this event. No email will be sent. Are you sure you want to continue?')"/>
                                                            </form></td>
HERE;
                                                            echo "</tr>";
                                                        }
                                                    }

                                                    ?>
                                        </tbody>

                                    </table>
                                    <form action="eventVolunteer.php">
                                    <br/><br/><br/><br/><input type="submit" class="get-trainer" value=""></form>
                                            <?php
                                        }
                                        ?>
                                    <br/><br/><br/><br/>
                                    <br/><br/><br/><br/>
                                    <!--My content is done-->

                                    <div id="details_content">
                                    </div>
                                    <!--  end table-content here -->
                                </div>
                                <div class="clear"></div>
                            </div>
                            <!--  end content-table-inner ............................................END  -->
                        </td>
                        <td id="tbl-border-right"></td>
                    </tr>
                    <tr>
                        <th class="sized bottomleft"></th>
                        <td id="tbl-border-bottom">&nbsp;</td>
                        <th class="sized bottomright"></th>
                    </tr>
                </table>
                <div class="clear">&nbsp;</div>

            </div>
            <!--  end content -->
            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content-outer........................................................END -->
            <?php
            footer();
            ?>

    </body>
</html>
    <?php
}else {
    header("Location: ../include/login.html");
}

?>